home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckPPDSanity.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  6.3 KB  |  169 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009, 2010 Red Hat, Inc.
  6. ## Authors:
  7. ##  Tim Waugh <twaugh@redhat.com>
  8.  
  9. ## This program is free software; you can redistribute it and/or modify
  10. ## it under the terms of the GNU General Public License as published by
  11. ## the Free Software Foundation; either version 2 of the License, or
  12. ## (at your option) any later version.
  13.  
  14. ## This program is distributed in the hope that it will be useful,
  15. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ## GNU General Public License for more details.
  18.  
  19. ## You should have received a copy of the GNU General Public License
  20. ## along with this program; if not, write to the Free Software
  21. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. import cups
  24. import cupshelpers
  25. import installpackage
  26. import os
  27. import subprocess
  28. from timedops import TimedOperation, TimedSubprocess
  29. from base import *
  30.  
  31. class CheckPPDSanity(Question):
  32.     def __init__ (self, troubleshooter):
  33.         Question.__init__ (self, troubleshooter, "Check PPD sanity")
  34.         vbox = gtk.VBox ()
  35.         vbox.set_border_width (12)
  36.         vbox.set_spacing (12)
  37.         self.label = gtk.Label ()
  38.         self.label.set_line_wrap (True)
  39.         self.label.set_use_markup (True)
  40.         self.label.set_alignment (0, 0)
  41.         vbox.pack_start (self.label, False, False, 0)
  42.  
  43.         box = gtk.HButtonBox ()
  44.         box.set_layout (gtk.BUTTONBOX_START)
  45.         self.install_button = gtk.Button (_("Install"))
  46.         box.add (self.install_button)
  47.         # Although we want this hidden initially,
  48.         # troubleshooter.new_page will call show_all() on the widget
  49.         # we give it.  We'll need to hide this button in the display()
  50.         # callback instead.
  51.         vbox.pack_start (box, False, False, 0)
  52.  
  53.         troubleshooter.new_page (vbox, self)
  54.  
  55.     def display (self):
  56.         self.answers = {}
  57.  
  58.         answers = self.troubleshooter.answers
  59.         if not answers['cups_queue_listed']:
  60.             return False
  61.  
  62.         parent = self.troubleshooter.get_window ()
  63.         name = answers['cups_queue']
  64.         tmpf = None
  65.         try:
  66.             cups.setServer ('')
  67.             self.op = TimedOperation (cups.Connection, parent=parent)
  68.             c = self.op.run ()
  69.             self.op = TimedOperation (c.getPPD, args=(name,), parent=parent)
  70.             tmpf = self.op.run ()
  71.         except RuntimeError:
  72.             return False
  73.         except cups.IPPError:
  74.             return False
  75.  
  76.         self.install_button.hide ()
  77.         title = None
  78.         text = None
  79.         try:
  80.             ppd = cups.PPD (tmpf)
  81.             self.answers['cups_printer_ppd_valid'] = True
  82.  
  83.             def options (options_list):
  84.                 o = {}
  85.                 for option in options_list:
  86.                     o[option.keyword] = option.defchoice
  87.                 return o
  88.  
  89.             defaults = {}
  90.             for group in ppd.optionGroups:
  91.                 g = options (group.options)
  92.                 for subgroup in group.subgroups:
  93.                     g[subgroup.name] = options (subgroup.options)
  94.                 defaults[group.name] = g
  95.             self.answers['cups_printer_ppd_defaults'] = defaults
  96.         except RuntimeError:
  97.             title = _("Invalid PPD File")
  98.             self.answers['cups_printer_ppd_valid'] = False
  99.             try:
  100.                 self.op = TimedSubprocess (parent=parent,
  101.                                            args=['cupstestppd', '-rvv', tmpf],
  102.                                            close_fds=True,
  103.                                            stdin=file("/dev/null"),
  104.                                            stdout=subprocess.PIPE,
  105.                                            stderr=subprocess.PIPE)
  106.                 result = self.op.run ()
  107.                 self.answers['cupstestppd_output'] = result
  108.                 text = _("The PPD file for printer '%s' does not conform "
  109.                          "to the specification.  "
  110.                          "Possible reason follows:") % name
  111.                 text += '\n' + reduce (lambda x, y: x + '\n' + y, result[0])
  112.             except OSError:
  113.                 # Perhaps cupstestppd is not in the path.
  114.                 text = _("There is a problem with the PPD file for "
  115.                          "printer '%s'.") % name
  116.  
  117.         if tmpf:
  118.             os.unlink (tmpf)
  119.  
  120.         if title == None and not answers['cups_printer_remote']:
  121.             (pkgs, exes) = cupshelpers.missingPackagesAndExecutables (ppd)
  122.             self.answers['missing_pkgs_and_exes'] = (pkgs, exes)
  123.             if len (pkgs) > 0 or len (exes) > 0:
  124.                 title = _("Missing Printer Driver")
  125.                 if len (pkgs) > 0:
  126.                     try:
  127.                         self.packagekit = installpackage.PackageKit ()
  128.                     except:
  129.                         pkgs = []
  130.  
  131.                 if len (pkgs) > 0:
  132.                     self.package = pkgs[0]
  133.                     text = _("Printer '%s' requires the %s package but it "
  134.                              "is not currently installed.") % (name,
  135.                                                                self.package)
  136.                     self.install_button.show ()
  137.                 else:
  138.                     text = _("Printer '%s' requires the '%s' program but it "
  139.                              "is not currently installed.") % (name,
  140.                                                                (exes + pkgs)[0])
  141.  
  142.         if title != None:
  143.             self.label.set_markup ('<span weight="bold" size="larger">' +
  144.                                    title + '</span>\n\n' + text)
  145.  
  146.         return title != None
  147.  
  148.     def connect_signals (self, handle):
  149.         self.button_sigid = self.install_button.connect ("clicked",
  150.                                                          self.install_clicked)
  151.  
  152.     def disconnect_signals (self):
  153.         self.install_button.disconnect (self.button_sigid)
  154.  
  155.     def collect_answer (self):
  156.         return self.answers
  157.  
  158.     def cancel_operation (self):
  159.         self.op.cancel ()
  160.  
  161.     def install_clicked (self, button):
  162.         pkgs = self.answers.get('packages_installed', [])
  163.         pkgs.append (self.package)
  164.         self.answers['packages_installed'] = pkgs
  165.         try:
  166.             self.packagekit.InstallPackageName (0, 0, self.package)
  167.         except:
  168.             pass
  169.